//
//  StashHeader.swift
//  Box
//
//  Created by mn on 2025/3/28.
//

import UIKit
import SnapKit

class StashHeader: UICollectionReusableView {
    static let reuseIdentifier = "StashHeader"

    private let imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.image = UIImage(named: "mine_stash")
        return imageView
    }()

    private let stackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.spacing = 10
        return stackView
    }()

    private var buttons: [UIButton] = []

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupViews()
    }

    private func setupViews() {
        addSubview(imageView)
//        addSubview(stackView)

        imageView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }

//        stackView.snp.makeConstraints { make in
//            make.left.equalToSuperview().offset(10)
//            make.right.equalToSuperview().offset(-10)
//            make.bottom.equalToSuperview().offset(-10)
//            make.height.equalTo(28)
//        }

//        ["全部", "青铜", "黄金", "铂金", "钻石"].forEach { title in
//            let button = UIButton(type: .custom)
//            button.setTitle(title, for: .normal)
//            button.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .medium)
//            button.setTitleColor(.fromHex("#001C00"), for: .normal)
//            button.setTitleColor(.white, for: .selected)
//            button.backgroundColor = UIColor.fromHex("#DBE8E6")
//            button.layer.cornerRadius = 14
//            button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
//            stackView.addArrangedSubview(button)
//            buttons.append(button)
//        }
//        buttonTapped(buttons.first!)
    }

    @objc private func buttonTapped(_ sender: UIButton) {
        buttons.forEach { button in
            let isSelected = (button == sender)
            button.isSelected = isSelected
            button.backgroundColor = isSelected ? UIColor.fromHex("#232321") : UIColor.fromHex("#DBE8E6")
            button.titleLabel?.font = isSelected ? UIFont.boldSystemFont(ofSize: 16) : UIFont.systemFont(ofSize: 16, weight: .medium)
        }
    }
}
